<div class="posts view">
<div class="widget" id="widget_post_view">
    <div class="toggle">&nbsp;</div>
    <h3 class="handle"><?php __('View Post');?></h3>
    <div class="content">
        <div class="post">
            <h3 class="title"><?php e($post['Post']['title']); ?></h3>
            <div class="meta ancher_parent">
                <a name="post">&nbsp;</a>
                <span class="created">
                    <?php __('Published:'); ?>
                    <?php e(date('Y-m-d', strtotime($post['Post']['created']))); ?>
                </span>
                <span class="author">
                    <?php __('Author:'); ?>
                    <?php e(ife(empty($post['CreatedUser']['nickname']), $post['CreatedUser']['username'], $post['CreatedUser']['nickname'])); ?>
                </span>
                <span class="clicks">
                    <?php __('Clicks:'); ?>
                    <?php e($post['Post']['clicks']); ?>
                </span>
                <?php if(!empty($user['group']) && $user['group'] != 'student'): ?>
                    <span class="edit"><?php e($html->link(__('Edit Post', true),
                        array('action' => 'edit', $post['Post']['id']))); ?></span>
                <?php endif; ?>
                <!-- Ancher section -->
                <div class="anchors">
                    <a href="#comment" class="comment" title="Comments">&nbsp;</a>
                    <a href="#attachment" class="attachment" title="Attachments">&nbsp;</a>
                </div>
            </div>
            <div class="content"><?php e($post['Post']['content']); ?></div>
            <!-- Comments section -->
            <?php if($post['Post']['commentable']): ?>
            <div class="comments ancher_parent">
                <a name="comment">&nbsp;</a>
                <div class="metas">
                    <h3><?php printf(__('Comments(%d)', true), $post['Post']['comment_count']); ?></h3>
                    <!-- Ancher section -->
                    <div class="anchors">
                        <a href="#post"  class="post" title="Post">&nbsp;</a>
                        <a href="#attachment" class="attachment" title="Attachments">&nbsp;</a>
                    </div>
                    <br class="clear"/>
                </div>
                <!-- Print comment list if there is one -->
                <div id="comment_list">
                <?php if($post['Post']['comment_count']):
                    $comments = $post['Comment']; ?>
                    <?php foreach($comments as $i=>$comment): ?>
                        <div class="comment">
                            <p class="meta">
                                <span class="counter"><?php e($i+1); ?></span>
                                <strong class="author <?php e($comment['User']['username']); ?>"><?php e($comment['User']['username']); ?></strong><br />
                                <small class="created"><?php e($time->format($options['longDateFormat'], $comment['created'])); ?></small>
                                <br class="clear"/>
                            </p>
                            <p class="content comment_by_<?php e($comment['User']['username']); ?>"><?php e($comment['content']); ?></p>
                        </div>
                    <?php endforeach; ?>
                <?php else: ?>
                    <span id="span_no_comment"><?php __('No comments yet'); ?></span>
                <?php endif; ?>
                </div>
                <!-- Print comment add form if there is logged in user -->
                <div class="form" id="comment_form">
                <?php if(!empty($user['id'])): ?>
                    <?php e($html->link(__('I want to say something', true), 'javascript:void(0)', array('id'=>'a_toggle_comment_form'))); ?>
                    <?php e($form->create('Comment', array('controller'=>'comments', 'action'=>'add')));?>
                        <fieldset>
                            <?php e($form->input('content', array('label'=>false, 'type'=>'textarea', 'class'=>'textarea'))); ?>
                            <?php e($form->hidden('post_id', array('value'=>$post['Post']['id']))); ?>
                            <?php e($form->hidden('user_id', array('value'=>$user['id']))); ?>
                            <?php e($form->submit(__('Add Comment', true), array('class'=>'submit attention'))); ?>
                        </fieldset>
                    <?php e($form->end()); ?>
                <?php else: ?>
                    <p>
                        <?php __('Login to post a comment'); ?>
                        <?php e($html->link(__('Login', true), array('controller' => 'users', 'action' => 'login'))); ?>
                    </p>
                <?php endif; ?>
                </div><br class="clear"/>
            </div>
            <?php endif; ?>
            <!-- Attachemnt section -->
            <?php if(count($post['Attachment'])):
                $attachments = $post['Attachment']; ?>
            <div class="attachments ancher_parent">
                <a name="attachment">&nbsp;</a>
                <div class="metas">
                    <h3><?php printf(__('Attachments(%d)', true), $post['Post']['attachment_count']); ?></h3>
                    <!-- Ancher section -->
                    <div class="anchors">
                        <a href="#post" class="post" title="Post">&nbsp;</a>
                        <a href="#comment" class="comment" title="Comments">&nbsp;</a>
                    </div><br class="clear"/>
                </div>
                <ul>
                <?php foreach($attachments as $i=>$item): ?>
                    <li>
                        <?php
                        if($file = $medium->file($item)){
                            $Medium = Medium::factory($file);
                            $size = $medium->size($file);
                            $size = $number->toReadableSize($size);
                            $pieces = explode('.', $item['basename']);
                            $extension = $pieces[count($pieces)-1];
                            $name = empty($item['alternative']) ? $item['basename'] : $item['alternative'];
                            printf('<span>%s&nbsp;(%s)</span>',
                                    $html->link(
                                        empty($extension) ? $name : $name.'.'.$extension,
                                        array('controller'=>'posts','action'=>'download', $item['id']),
                                        array('title'=>__('download attachment', true))),
                                    $size);
                        }
                        ?>
                    </li>
                <?php endforeach; ?>
                </ul>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php //unset($post['Post']['content']); debug($post);  ?>
</div>
<script type="text/javascript">
jQuery(function(){
    /* Validate CommentAddForm */
    $('#CommentAddForm').submit(function(event){
        if(!$(this).valid()) return false;
        event.preventDefault();
        $.sme.ajaxStart = '正在提交您的评论, 稍后';
        $(this).ajaxSubmit({
            global: true,
            dataType: 'json',
            success: function(json){
                $.sme.autoNotify(json);
                $.sme.cbk.ajaxCommentSuccess.apply(json);
            }
        });
        return false;
    }).validate({
        rules: { 'data[Comment][content]': 'required' },
        messages: { 'data[Comment][content]': '评论为空时不得提交' }
    });
    /* toggle comment add form with a link */
    $('a#a_toggle_comment_form').click(function(){
        $('#CommentAddForm').toggle();
    }).click();
});

$.extend($.sme.cbk, {
    /* Perform comment list update task when add success */
    ajaxCommentSuccess: function(){
        if(this.status && this.data.Comment){
            $('span#span_no_comment').remove();
            //$('div#comment_list').append($.sme.cbk.createNewComment(this.data));
            var element = $.sme.cbk.createNewComment(this.data);
            element.hide().appendTo('div#comment_list').fadeIn(2000);
        }
        $('#CommentAddForm').clearForm();
    },
    /* create and comment element dynamicly with the specified data */
    createNewComment: function(data){
        var div = $('<div class="comment"></div>');
        //if(!data.Comment.id || data.User) return div;
        var comments = $('div.comment');
        var meta = $('<p class="meta"></p>')
            .append('<span class="counter">'+(comments.length+1)+'</span>')
            .append('<strong class="author '+data.User.username+'">'+data.User.username+'</strong><br />')
            .append('<small class="created">'+data.Comment.created+'</small><br class="clear"/>');
        var content = $('<p class="content comment_by_'+data.User.username+'">'+data.Comment.content+'</p>');
        return div.append(meta).append(content);
    }
});
</script>